JBoss Community Archive (Read Only)

Infinispan 5.0

Getting Started Guide - Embedded Cache in Java SE

Running Infinispan in embedded mode is very easy. First, we'll set up a project, and then we'll run Infinispan, and start adding data.

embedded-cache quickstart

All the code discussed in this tutorial is available in the embedded-cache quickstart.

Creating a new Infinispan project

The only thing you need to set up Infinispan is add it's dependencies to your project. If you are using Maven (or another build system like Gradle or Ivy which can use Maven dependencies), then this is easy. Just add:

pom.xml snippet
013.   ~ This software is distributed in the hope that it will be useful,
014.   ~ but WITHOUT ANY WARRANTY; without even the implied warranty of
015.   ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016.   ~ Lesser General Public License for more details.
017.   ~

to your <dependencies> section of the POM. You'll need to substitute ${infinispan.version} for the version of Infinispan you wish to use.

Which version of Infinispan should I use?

We recommend using the latest final version of Infinispan. All releases are displayed on the downloads page.

You'll also need to enable the JBoss Maven repository. We recommend adding a profile:

pom.xml snippet
022.   -->
023. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
024.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
025.    <modelVersion>4.0.0</modelVersion>
026.    <groupId>org.infinispan.quickstart</groupId>
027.    <artifactId>embedded-cache-quickstart</artifactId>
028.    <version>7.0.0-SNAPSHOT</version>
029. 
030.    <name>Infinispan Embedded Cache Quickstart</name>
031.    <description>This quickstart demonstrates embedded cache running on a single node in Java SE.</description>
032. 
033.    <url>http://jboss.org/infinispan</url>
034.    <licenses>
035.       <license>
036.          <name>GNU Lesser General Public License</name>
037.          <url>http://www.gnu.org/copyleft/lesser.html</url>
038.          <distribution>repo</distribution>
039.       </license>
040.    </licenses>
041. 
042.    <properties>
043.       <!-- Explicitly declaring the source encoding eliminates the following
044.          message: -->
045.       <!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
046.          resources, i.e. build is platform dependent! -->
047.       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
048. 
049.       <infinispan.version>7.1.0.Final</infinispan.version>
050.    </properties>
051. 
052.    <dependencies>
053.       <dependency>
054.          <groupId>org.infinispan</groupId>
055.          <artifactId>infinispan-embedded</artifactId>
056.          <version>${infinispan.version}</version>
057.       </dependency>
058.    </dependencies>
059. 
060.    <build>

Alternatively, you can use the POM from the quickstart that accompanies this tutorial.

If you are using Ant, or another build system which doesn't provide declarative dependency management, then the Infinispan distribution zip contains a lib/ directory. Add the contents of this to the build classpath.

Running Infinispan on a single node

In order to run Infinispan, we're going to create a main method in the Quickstart class. Infinispan comes configured to run out of the box; once you have set up your dependencies, all you need to do to start using Infinispan is to create a new cache manager and get a handle on the default cache.Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/Quickstart.java status code: 404.

We now need a way to run the main method! If you are using Maven, the best approach is to copy all the project dependencies to a directory, and at the same time compile the java classes from our project:

$> mvn clean compile dependency:copy-dependencies -DstripVersion

Having done that, we can run the main method:

$> java -cp target/classes/:target/dependency/* Quickstart

You should see Infinispan start up, and the version in use logged to the console.

Congratulations, you now have Infinispan running as a local cache!

Use the default cache

Infinispan exposes a Map-like, JSR-107-esque interface for accessing and mutating the data stored in the cache. For example:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/DefaultCacheQuickstart.java status code: 404.

Infinispan offers a thread-safe data-structure:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/DefaultCacheQuickstart.java status code: 404.

By default entries are immortal but you can override this on a per-key basis and provide lifespans.Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/DefaultCacheQuickstart.java status code: 404.

Use a custom cache

Each cache in Infinispan can offer a different set of features (for example transaction support, different replication modes or support for eviction), and you may want to use different caches for different classes of data in your application. To get a custom cache, you need to register it with the manager first:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/CustomCacheQuickstart.java status code: 404.

The example above uses Infinispan's fluent configuration, which offers the ability to configure your cache programmatically. However, should you prefer to use XML, then you may. We can create an identical cache to the one created with a programmatic configuration:

infinispan.xml
24. <infinispan
25.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
26.         xsi:schemaLocation="urn:infinispan:config:7.0 http://www.infinispan.org/schemas/infinispan-config-7.0.xsd"
27.         xmlns="urn:infinispan:config:7.0">
28. 
29.    <cache-container default-cache="default">
30.        <local-cache name="xml-configured-cache">
31.           <eviction strategy="LIRS" max-entries="10" />
32.        </local-cache>
33.    </cache-container>

We then need to load the configuration file, and use the programmatically defined cache:Code Snippet error: Unable to retrieve the URL: https://github.com/infinispan/infinispan-quickstart/raw/master/embedded-cache/src/main/java/XmlConfiguredCacheQuickstart.java status code: 404.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-11 09:07:54 UTC, last content change 2011-08-04 15:46:29 UTC.